package edu.fiu.ann.utility;

/**
 * Normalize the data between 0 and 1.
 *
 * @author Yexi Jiang (http://users.cis.fiu.edu/~yjian004/)
 * @date 2011-4-4
 */
public class NormalizeData {
	
	private double[] min;
	private double[] max;
	
	private int dataSize;
	private int dimension;
	
	private double[][] result;
	
	public NormalizeData(double[][] dataset){
		
		int dataSize = dataset.length;
		int dimension = dataset[0].length;
		
		for(double[] instance : dataset){
			for(int i = 0; i < instance.length; ++i){
				if(instance[i] < min[i]){
					min[i] = instance[i];
				}
				if(instance[i] > max[i]){
					max[i] = instance[i];
				}
			}
		}
		
		normalizeResult(dataset);
	}
	
	private double[][] normalizeResult(double[][] dataset){
		double[][] result = new double[dataSize][dimension];
		
		for(int j = 0; j < dimension; ++j){
			double diff = max[j] - min[j];
			for(int i = 0; i < dataSize; ++i){
				result[i][j] = (dataset[i][j] - min[j]) / diff;
			}
		}
		
		return result;
	}
}
